package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/m"
	"GameServer/Game/sp"

	log "github.com/sirupsen/logrus"
)

func (p *Player) EventMsgListRequest(req *sp.EventMsgListRequest) (res *sp.EventMsgListResponse) {
	rList, err := m.NewUserEventMsg(p.User.Uid).GetEventMsgList()
	if err != nil {
		res = &sp.EventMsgListResponse{}
		return
	}
	res = &sp.EventMsgListResponse{
		List: lib.FormatEventMsgSps(rList),
	}
	return
}

// 60010 使用炸弹
func (p *Player) UseBombRequest(req *sp.UseBombRequest) (res *sp.UseBombResponse) {
	targetGridId, gridsNeedToOpen, grids, err := lib.UseBomb(req.GridId, p.User)
	if err != nil {
		res = &sp.UseBombResponse{
			Errcode: errors.ErrCode(err),
		}
		return res
	}
	var openGridNumResponse int64
	var storyStatusResponse int64
	var openAltarResponse int64
	for _, gridId := range gridsNeedToOpen {
		//逐个开格子
		mGrids, _, openAltar, uProp, openGridNum, storyStatus, _, err := lib.OpenGrid(gridId, p.User, true, true)
		if err != nil {
			res = &sp.UseBombResponse{
				Errcode: errors.ErrCode(err),
			}
			return res
		}
		if openGridNum != 0 {
			openGridNumResponse = openGridNum
		}
		if storyStatus != 0 {
			storyStatusResponse = storyStatus
		}
		if openAltar {
			openAltarResponse = 1
		}
		// 是烧焦的格子 而且 是第一次打开
		// if isFireGrid && !p.User.IsOpenFireGrid() {
		// 	p.User.OpenFireGrid()
		// 	// p.User.RemoveFireProtec()
		// 	// slot.RemoveFireProtec(p.User.Uid.Hex())
		// }
		p.TimelyUpdateProp(req.GetType(), uProp)
		utils.AsyncFunc(func() {
			p.UpdateProp(req.GetType(), uProp)
		})

		if p.User.UnionId != "" && openAltar {
			uProp1, _, _, err := lib.GiveGoodsByKv("UseBombRequest", p.User, m.BaseKV{Key: data.GlobalEnum.Union, Val: 1}, p.Send)
			if err != nil {
				log.WithFields(log.Fields{
					"data":   m.BaseKV{Key: data.GlobalEnum.Union, Val: 1},
					"uid":    p.User.Uid.Hex(),
					"method": "UseBombRequest",
				}).Error("给公会积分失败")
			} else {
				p.TimelyUpdateProp(req.GetType(), uProp1)
				utils.AsyncFunc(func() {
					p.UpdateProp(req.GetType(), uProp1)
				})
			}
		}

		cntGrids := int64(0)
		for _, grid := range mGrids {
			grids[grid.Id] = grid
			cntGrids++
		}
		// 成就，记录使用炸弹，炸开格子数
		lib.AddAchievProgress(p.User, data.AchievementType.Pick_bomb, cntGrids)
	}
	// 有委托完成
	if num := lib.EntrustCheck(p.User); num > 0 {
		utils.AsyncFunc(func() {
			p.Send(&sp.EntrustNoticeRequest{
				Num: num,
			})
		})
	}

	// 有成就完成，进行通知
	achLst := lib.AchievComplete(p.User)
	if len(achLst) > 0 {
		p.Send(&sp.AchievementNoticeRequest{
			Lst: achLst,
		})
	}

	grids, _ = lib.MonsterLockBrick(p.User, grids)

	res = &sp.UseBombResponse{
		GridId:      req.GridId,
		BombGridId:  targetGridId,
		GridList:    lib.FormartGridInfoSps(grids),
		OpenGridNum: openGridNumResponse,
		StoryStatus: storyStatusResponse,
		OpenAltar:   openAltarResponse,
	}
	return
}

// 60011 使用漂流瓶
func (p *Player) UseDriftingBottleRequest(req *sp.UseDriftingBottleRequest) (res *sp.UseDriftingBottleResponse) {
	us := p.User
	gridInfo, kvsGoods, resGoods, err := lib.UseDriftingBottle(req.GridId, us)
	if err != nil {
		log.WithFields(log.Fields{
			"req.GridId": req.GridId,
			"err":        err,
			"method":     "UseDriftingBottleRequest",
		}).Error("使用漂流瓶失败")
		return
	}
	// 好友uid
	friendUids := lib.GetFriendsUidLst(us)
	// 推荐好友
	recom := lib.FindRecomFriendOne(us, 0, 30, friendUids)
	if recom == nil {
		recom = lib.FindRecomFriendOne(us, 1, 30, friendUids)
		// 这时候没有数据，就真的没有数据
	}

	p.PutData("kvsGoods", kvsGoods)

	// 奖励道具
	res = &sp.UseDriftingBottleResponse{
		GridId:   req.GridId,
		UserData: lib.FormatSimpleFriend(recom),
		Rewards:  lib.FormatGoodsInfoSps(resGoods),
		GridInfo: lib.FormartGridInfoSp(gridInfo),
	}
	return
}

// 60012 漂流瓶，加好友领道具
func (p *Player) DriftingBottleRewardRequest(req *sp.DriftingBottleRewardRequest) (res *sp.DriftingBottleRewardResponse) {
	us := p.User
	val, ok := p.GetData("kvsGoods")
	if !ok {
		log.WithFields(log.Fields{
			"method": "DriftingBottleRewardRequest",
		}).Error("好友奖励，获取缓存数据失败。")
		res = &sp.DriftingBottleRewardResponse{
			Errcode: data.Errcode.ServerErr,
		}
		return
	}
	valT := val.([]m.BaseKV)

	uProp, resGoods, boxResList, err := lib.GiveGoodsByKvList("DriftingBottleRewardRequest", us, valT, p.Send)
	if err != nil {
		log.WithFields(log.Fields{
			"err":    err,
			"method": "DriftingBottleRewardRequest",
		}).Error("好友奖励获得失败")
		res = &sp.DriftingBottleRewardResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	err = lib.ReqMakeFriendsWithPlayer(us, req.Uid)
	// err = lib.MakeFriendsWithPlayer(us, req.Uid)
	if err != nil {
		log.WithFields(log.Fields{
			"err":    err,
			"method": "DriftingBottleRewardRequest",
		}).Error("强制加好友失败")
		res = &sp.DriftingBottleRewardResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})

	res = &sp.DriftingBottleRewardResponse{
		Res:    lib.FormatGoodsInfoSps(resGoods),
		BoxRes: lib.FormatBoxResSps(boxResList),
	}
	return
}
